package com.example.presenters;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.interfaces.IAdminUserActivity;
import com.example.model.User;
import com.example.model.UserList;

/**
 * This presenter manages an administrator's view.
 * 
 * @author 3Factorial
 */
public class AdminUserPresenter {

	private IAdminUserActivity activity;
	private User user;

	/**
	 * Initiates the presenter.
	 * 
	 * @param activity
	 *            The activity to use with this presenter
	 * @param users
	 *            The list of currently active users
	 * @param username
	 *            The current logged-in user
	 */
	public AdminUserPresenter(IAdminUserActivity activity, UserList users,
			String username) {
		this.activity = activity;
		this.user = users.getUserByUsername(username);
	}

	/**
	 * Sets the view to provide the correct info.
	 * 
	 * @param userTypeText
	 *            Text to prepare.
	 * @param userTypeButton
	 *            Button to prepare.
	 */
	public void setUserType(TextView userTypeText, Button userTypeButton) {
		// TODO This needs to happen at the view level.
		if (user.isAdmin()) {
			userTypeText.setText("Administrator");
			userTypeButton.setText("Disable Administrator Status");
		} else {
			userTypeText.setText("Standard User");
			userTypeButton.setText("Make Administrator");
		}
	}

	/**
	 * Changes the user to an administrator or vice versa.
	 * 
	 * @param userTypeText
	 * @param userTypeButton
	 */
	public void changeUserType(TextView userTypeText, Button userTypeButton) {
		if (user.isAdmin()) {
			user.setIsAdmin(false);
		} else {
			user.setIsAdmin(true);
		}
		setUserType(userTypeText, userTypeButton);
	}

	/**
	 * Sets the view to display that a certain user is locked.
	 * 
	 * @param isLockedText
	 * @param isLockedButton
	 */
	public void setIsLocked(TextView isLockedText, Button isLockedButton) {
		if (user.isLocked()) {
			isLockedText.setText("Account Locked");
			isLockedButton.setText("Unlock User");
		} else {
			isLockedText.setText("Account Unlocked");
			isLockedButton.setText("Lock User");

		}
	}

	/**
	 * Changes user locked status to false or true depending on whether it was
	 * locker or not, respectively.
	 * 
	 * @param isLockedText
	 * @param isLockedButton
	 */
	public void changeIsLocked(TextView isLockedText, Button isLockedButton) {
		if (user.isLocked())
			user.lock(false);
		else
			user.lock(true);
		setIsLocked(isLockedText, isLockedButton);
	}

	/**
	 * Deletes a user.
	 */
	public void deleteUser() {
		UserList.delete(user);
	}

}